<?php
include_once(dirname(__FILE__).'/../autoload.php');

class Acceptance_Controller_mainTest extends Lib_AcceptanceTestcase
{
    public function setUp()
    {
        ob_start();
    }
    public function teardown()
    {
        ob_end_clean();
    }

    public function test_checkSession_fails()
    {
        $main = self::initController('Main');
        $main->_checkSession();
        unset ($main);
        
        $expected = '{"success":false,"message":"Session invalid or not started"}';
        $actual = self::getOutput();
        
        $this->assertEquals($expected, $actual, 'Unexpected output');
    }

    public function test_checkSession_ok()
    {
        $main = self::initController('Main');
        $_SESSION['user'] = 'somefakedata';
        $main->_checkSession();
        unset ($main);

        $expected = '{"success":true,"message":"Session Ok"}';
        $actual = self::getOutput();

        $this->assertEquals($expected, $actual, 'Unexpected output');
    }

    public function test_login_fails()
    {
        $main = self::initController('Main', array(
            'username' => 'Dude',
            'password' => 'Wr0ng P4s5w0rcl'
        ));
        $main->_login();
        unset ($main);

        $expected = '{"success":false,"message":"Pleace check your username and password"}';
        $actual = self::getOutput();

        $this->assertEquals($expected, $actual, 'Failed at failing login');
    }

    public function test_login_ok()
    {
        $main = self::initController('Main', array(
            'username' => 'Jack',
            'password' => 'salasana'
        ));
        $main->_login();
        unset ($main);

        $expected = '{"success":true,"message":"Welcome Jack!","userId":"3"}';
        $actual = self::getOutput();

        $this->assertEquals($expected, $actual, 'Failed at failing login');
    }

    public function test_logout()
    {
        @session_start();
        $_SESSION['user'] = serialize(array('name' => 'bogususer'));
        $main = self::initController('Main');
        $main->_logout();
        unset($main);

        $expected = '{"success":true,"message":"Logged out","redirUrl":"\/"}';
        $actual = self::getOutput();
        $this->assertEquals($expected, $actual, 'Wrong message from logout');
    }

    public function test_jsElement()
    {
        $main = self::initController('Main', array(),
                array('script' => 'layout.ui.js'));
        $main->_jsElement();
        unset($main);

        $expected = file_get_contents(
                dirname(__FILE__).'/../../Source/View/Element/layout.ui.js');
        $actual = self::getOutput();

        $this->assertEquals($expected, $actual, 'Wrong or no .js-file returned');
    }

    public function test_jsFunctionality()
    {
        $main = self::initController('Main', array(),
                array('script' => 'globals.js'));
        $main->_jsFunctionality();
        unset($main);

        $expected = file_get_contents(
                dirname(__FILE__).'/../../Source/View/Functionality/globals.js');
        $actual = self::getOutput();

        $this->assertEquals($expected, $actual, 'Wrong or no .js-file returned');
    }

    public function test_index()
    {
        $_SERVER['SERVER_NAME'] = 'localhost';

        $main = self::initController('Main');
        $main->_index();
        unset($main);
        $expected = file_get_contents(dirname(__FILE__).'/mainTest/outputOfIndex.html');
        $actual = self::getOutput();


        $this->assertStringsEqualIgnoreEmptyLines($expected, $actual,
        //$this->assertEquals( $expected , $actual ,
                'Chanded or invalid output of index');
    }

}